<?php
namespace App\Services;


use App\Models\PayChannel;
use App\Models\PayScene;
use common\enum\CommonEnum;
use common\enum\MasterLogEnum;
use common\utils\SysCode;

class PayChannelService
{
    /**
     * 获取支付渠道列表
     * @param array $params
     *  [ 'id'=>渠道id, 'name'=>'渠道名', 'status'=>'渠道状态 0 无效 1 有效','page'=> 页码, $pageSize=>分页大小]
     * @return array
     */
    public static function getPayChannelList($params)
    {
        $where = [];
        if(!empty($params['id'])){
            $where['id'] = $params['id'];
        }
        if(isset($params['status']) && in_array($params['status'], [CommonEnum::DISABLE, CommonEnum::ENABLED])){
            $where['status'] = $params['status'];
        }
        $sqlObj = PayChannel::select(['id','name', 'logo','code','status','pay_scene_data', 'description','callback_url','api_params']);
        if (!empty($where)){
            $sqlObj->where($where);
        }
        if(!empty($params['name'])){
            $sqlObj->whereRaw("LOCATE('" . trim($params['name']) . "', name)");
        }
        $result = get_page_data($sqlObj, $params['page'], $params['page_size']);
        if (!empty($result['list'])){
            foreach ($result['list'] as &$v){
                $v['pay_scene_data'] = json_decode($v['pay_scene_data'], true);
                $v['api_params'] = json_decode($v['api_params'], true);
            }
        }
        return $result;
    }

    /**
     * 通过ID获取支付渠道详细
     * @param int $id
     * @return mixed
     */
    public static function getPayChannelInfo($id)
    {
        $result = PayChannel::find(['id'=>$id])->first();
        if (!empty($result)){
            $result['pay_scene_data'] = json_decode($result['pay_scene_data'], true);
            $result['api_params'] = json_decode($result['api_params'], true);
        }
        return $result;
    }

    /**
     * 通过ID获取支付渠道详细
     * @return mixed
     */
    public static function getPayChannel()
    {
        $res = PayScene::select(['scene', 'name'])->get()->toArray();
        $result = [];
        if(!empty($res)){
            foreach ($res as $v){
                $result[$v['scene']] = $v['name'];
            }
        }
        return $result;
    }

    public static function savePayChannelInfo($data)
    {
        // 新增支付渠道
        $pay = PayChannel::orwhere(['code'=>$data['code'], 'name'=>$data['name']]);
        if(!empty($data['id'])){
            $pay->where('id', '<>', $data['id']);
        }
        $pay = $pay->first();
        if(!empty($pay)){
            error_exit(SysCode::PAY_CHANNEL_EXIST);
        }
        if(empty($data['id'])){
            $payChannel = new PayChannel();
            $payChannel->name = (isset($data['name']) && !empty($data['name'])) ? $data['name'] : '';
            $payChannel->code = (isset($data['code']) && !empty($data['code'])) ? $data['code'] : '';
            $payChannel->pay_scene_data = (isset($data['pay_scene_data']) && !empty($data['pay_scene_data'])) ? json_encode($data['pay_scene_data']) : json_encode([]);
            $payChannel->api_params = (isset($data['api_params']) && !empty($data['api_params'])) ? json_encode($data['api_params']) : json_encode([]);
            $payChannel->status = (isset($data['status']) && $data['status']==CommonEnum::ENABLED) ? CommonEnum::ENABLED : CommonEnum::DISABLE;
            $payChannel->description = (isset($data['description']) && !empty($data['description'])) ? $data['description'] : '';
        }else{
            $payChannel = PayChannel::find(['id'=>$data['id']])->first();
            if (empty($payChannel)){
                error_exit(SysCode::INVALID_ARGUMENT);
            }
            (isset($data['name']) && !empty($data['name']) && $payChannel->name!=$data['name']) && $payChannel->name = $data['name'];
            (isset($data['code']) && !empty($data['code']) && $payChannel->code!=$data['code']) && $payChannel->code = $data['code'];
            (isset($data['pay_scene_data']) && !empty($data['pay_scene_data']) && $payChannel->pay_scene_data!=$data['pay_scene_data']) && $payChannel->pay_scene_data = json_encode($data['pay_scene_data']);
            (isset($data['api_params']) && !empty($data['api_params']) && $payChannel->api_params!=$data['api_params']) && $payChannel->api_params = json_encode($data['api_params']);
            (isset($data['status']) && in_array($data['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE]) && $payChannel->status!=$data['status']) && $payChannel->status = $data['status'];
            (isset($data['description']) && !empty($data['description']) && $payChannel->description!=$data['description']) && $payChannel->description = $data['description'];
        }
        $result = $payChannel->save();
        if (!$result) {
            error_exit(SysCode::FAILED);
        }
        if (empty($data['id'])) {
            $remark = str_replace('{:name}', $data['name'], MasterLogEnum::REMARK['pay_add']);
            MasterLogService::writeMasterLog(MasterLogEnum::PAY_ADD, MasterLogEnum::SUCCESS, $remark);
        } else {
            $remark = str_replace('{:name}', $data['name'], MasterLogEnum::REMARK['pay_edit']);
            MasterLogService::writeMasterLog(MasterLogEnum::PAY_EDIT, MasterLogEnum::SUCCESS, $remark);
        }
        return $result;
    }

    // 修改支付渠道开启关闭状态和通道的开启关闭
    public static function updatePayChannel($data)
    {
        $payChannel = PayChannel::find(['id'=>$data['id']])->first();
        if (empty($payChannel)){
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        (isset($data['status']) && in_array($data['status'], [CommonEnum::ENABLED, CommonEnum::DISABLE]) && $payChannel->status!=$data['status']) && $payChannel->status = $data['status'];
        (isset($data['pay_scene_data']) && !empty($data['pay_scene_data']) && $payChannel->pay_scene_data!=$data['pay_scene_data']) && $payChannel->pay_scene_data = json_encode($data['pay_scene_data']);
        return $payChannel->save();
    }

}